package com.woniu.supermarket.util;


import com.woniu.supermarket.vo.EmpVo;
import com.woniu.supermarket.vo.JwtInfo;
import io.jsonwebtoken.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

public class JwtUtil {
    private static Properties properties = new Properties();

   static{
       InputStream in = JwtUtil.class.getClassLoader().getResourceAsStream("jwt.properties");
       //把信息加载到集合中
       try {
           properties.load(in);
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

    /**
     * 生成token
     * @param empVo 自定义信息的对象
     * @return
     */
    public static String createToken(EmpVo empVo){
        try{
            //利用建造者模式
            JwtBuilder jwtBuilder = Jwts.builder();
            //设置头部
            jwtBuilder.setHeaderParam("alg", properties.getProperty("alg"));
            jwtBuilder.setHeaderParam("typ", properties.getProperty("typ"));

            String expiration = properties.getProperty("expiration");

            //设置有效载荷
            jwtBuilder.setIssuer(properties.getProperty("issuer"));
            jwtBuilder.setSubject(properties.getProperty("subject"));
            jwtBuilder.setExpiration(new Date(new Date().getTime()+Long.parseLong(expiration)));

            //设置自定义信息
            jwtBuilder.claim(properties.getProperty("userName"),empVo.getUsername());

            //设置签证
            jwtBuilder.signWith(SignatureAlgorithm.HS256,properties.getProperty("secret"));

            //生成token
            String token = jwtBuilder.compact();
            return token;
        }catch(RuntimeException e){
            e.printStackTrace();
            throw new RuntimeException("生成token出错");
        }

    }

    /**
     * 解析JWT的token
     * @param token
     * @return
     */
    public static Claims parseToken(String token){
        try{
            JwtParser jwtParser = Jwts.parser();
            Jws<Claims> claimsJws = jwtParser.setSigningKey(properties.getProperty("secret")).parseClaimsJws(token);
            Claims body = claimsJws.getBody();
            return body;
        }catch (ExpiredJwtException e){
            e.printStackTrace();
            throw new ExpiredJwtException(null,null,"token已经过期",e);
        }catch (SignatureException e1){
            e1.printStackTrace();
            throw new SignatureException("密钥不正确",e1);
        }catch (RuntimeException e2){
            e2.printStackTrace();
            throw new RuntimeException("解析token出错",e2);
        }
    }


    /**
     * 通过解析JWT的token
     * @param token
     * @return 返回用户名
     */
    public static String getUsername(String token){
        try{
            JwtParser jwtParser = Jwts.parser();
            Jws<Claims> claimsJws = jwtParser.setSigningKey(properties.getProperty("secret")).parseClaimsJws(token);
            Claims body = claimsJws.getBody();
            String userName = (String)body.get(properties.getProperty("userName"));
            return userName;
        }catch (ExpiredJwtException e){
            e.printStackTrace();
            throw new ExpiredJwtException(null,null,"token已经过期",e);
        }catch (SignatureException e1){
            e1.printStackTrace();
            throw new SignatureException("密钥不正确",e1);
        }catch (RuntimeException e2){
            e2.printStackTrace();
            throw new RuntimeException("解析token出错",e2);
        }
    }


}
